package com.tanxi521.springboot.controller;

import com.tanxi521.springboot.bean.OfficialMessageBean;
import com.tanxi521.springboot.pojo.APIResult;
import com.tanxi521.springboot.pojo.DeleteIds;
import com.tanxi521.springboot.pojo.OfficialMessage;
import com.tanxi521.springboot.pojo.RestPoint;
import com.tanxi521.springboot.service.InGameMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: i-xiaotian
 * @Date: 2022/4/10 15:36
 * @EMAIL: 1254806967@qq.com
 */

@RestController
@RequestMapping("/game")
@CrossOrigin
public class InGameMessageController {

    @Resource
    InGameMessageService inGameMessageService;

    @GetMapping("/message/official")
    @RestPoint
    public APIResult getOfficialMessage(Integer pageSize, Integer pageIndex) {
        List<OfficialMessage> officialMessageList = inGameMessageService.queryOfficialMessage(pageSize, pageIndex);
        int count = inGameMessageService.queryMessageCount();
        OfficialMessageBean officialMessageBean = new OfficialMessageBean();
        officialMessageBean.setCount(count);
        officialMessageBean.setPageCount(count / pageSize + 1);
        officialMessageBean.setOfficialMessageList(officialMessageList);
        return new APIResult(officialMessageBean, APIResult.SUCCESS, "查询游戏内轮播消息成功");
    }

    @PostMapping("/message/official")
    @RestPoint
    public APIResult addOfficialMessage(String createBy, String message) {
        int count = inGameMessageService.insertOfficialMessage(createBy, message);
        return new APIResult(APIResult.SUCCESS, "增加轮播公告成功");
    }

    @DeleteMapping("/message/official")
    @RestPoint
    public APIResult deleteOfficialMessage(@RequestBody DeleteIds ids) {
        int count = inGameMessageService.deleteOfficialMessage(ids.getIds());
        if (count != 0) {
            return new APIResult(APIResult.SUCCESS, "删除成功");
        } else {
            return new APIResult(APIResult.FAIL, "删除失败或已删除");
        }
    }

}
